package com.example.asus.diary_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;
import org.litepal.util.Const;

import java.util.List;

public class test extends AppCompatActivity {
    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Connector.getDatabase();
            }
        });
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Diary diary = new Diary();
                diary.setTitle("today");
                diary.setBody("ddd");
                diary.save();

                diary.setTitle("haha");
                diary.setBody("asd");
                diary.save();
            }
        });

        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Diary diary = new Diary();
                diary.setId(3);
                diary.setTitle("test");
                diary.setBody("ttt");
                diary.save();
                diary.setBody("12345");
                diary.save();
            }
        });

        Button deleteButton = (Button) findViewById(R.id.delete_data);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Diary.class);
            }
        });

        Button queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Diary> diaries = DataSupport.findAll(Diary.class);
                for(Diary diary : diaries){
                    Log.d(TAG, "diary id is "+diary.getId());
                    Log.d(TAG, "diary title is "+diary.getTitle());
                    Log.d(TAG, "diary title is "+diary.getBody());
                }

                Diary firstbook = DataSupport.findFirst(Diary.class);//查找Book表中的第一条数据

                Diary lastbook = DataSupport.findLast(Diary.class);//查找Book表中的最后一条数据

            }
        });
    }
}
